<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>平时成绩管理系统-班级管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">班级编号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="clazzId" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">班级名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="clazzName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">年级</label>
                    <div class="layui-input-inline">
                        <input type="text" name="clazzYear" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">教工号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="teacherId" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">班主任名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="teacherName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-comm" data-type="reload" lay-submit
                            lay-filter="LAY-app-clazz-search" id="search-btn">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-header layuiadmin-card-header-auto">
            <button class="layui-btn layuiadmin-btn-tags" data-type="add" id="add-btn">新增班级信息</button>
        </div>
        <div class="layui-card-body">
            <table id="LAY-app-clazz" lay-filter="LAY-app-clazz"></table>
            <script type="text/html" id="layuiadmin-app-clazz-bar">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script>

    function serializeArrayToJson(dataArr) {
        let dataMap = new Map();
        for (let item of dataArr) {
            dataMap.set(item.name, item.value);
        }

        if (dataMap.has("enable")) {
            dataMap.set("enable", 1);
        } else {
            dataMap.set("enable", 0);
        }
        let json = {};
        for (let [k, v] of dataMap) {
            json[k] = v;
        }
        return JSON.stringify(json);
    }


    layui.config({
        base: '../../layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'clazz', 'table','form'], function () {

        let table = layui.table;
        let form = layui.form;
        let $ = layui.$, active = {
            // 添加
            add: function () {
                layer.open({
                    type: 2,
                    title: '新增客户端',
                    content: 'clazz_add.html',
                    area: ['30%', '60%'],
                    btn: ['提交', '取消'],
                    btnAlign: 'c',
                    yes: function (index) {
                        let dataArr = layer.getChildFrame("#addClazz").serializeArray();
                        //添加
                        $.ajax({
                            url: layui.setter.baseUrl + '/clazz/add',
                            type: 'post',
                            data: serializeArrayToJson(dataArr),
                            dateType: "json",
                            contentType: 'application/json',
                            success: function (res) {
                                if (res.code === 200) {
                                    layer.msg("成功");
                                    layer.close(index);
                                    //刷新
                                    $(".layui-laypage-btn")[0].click();
                                } else {
                                    layer.msg("失败");
                                }
                            }
                        });
                    }
                });
            },
            // 搜素
            reload: function () {
                let params = {
                    clazzId: $("input[name='clazzId']").val(),
                    clazzName: $("input[name='clazzName']").val(),
                    clazzYear: $("input[name='clazzYear']").val(),
                    teacherId: $("input[name='teacherId']").val(),
                    teacherName: $("input[name='teacherName']").val(),
                };

                //执行重载
                table.reload('LAY-app-clazz', {
                    page: {
                        curr: 1
                    }
                    , where: params
                });
            }

        };
        $('#add-btn').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        $('#search-btn').on('click', function () {
            let type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
